package com.xinfu.newproject.ui.carFee.add_parking;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.parkingwang.vehiclekeyboard.KeyboardInputController;
import com.parkingwang.vehiclekeyboard.PopupKeyboard;
import com.parkingwang.vehiclekeyboard.core.KeyboardEntry;
import com.parkingwang.vehiclekeyboard.core.KeyboardType;
import com.parkingwang.vehiclekeyboard.view.InputView;
import com.parkingwang.vehiclekeyboard.view.KeyboardCallback;
import com.xinfu.newproject.R;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.bean.park_bean.bean.AddMonthCarBean;
import com.xinfu.newproject.bean.park_bean.bean.CheckMonthParkBean;
import com.xinfu.newproject.bean.park_bean.bean.FeeListBean;
import com.xinfu.newproject.bean.park_bean.bean.GetMonthCarListBean;
import com.xinfu.newproject.bean.park_bean.bean.GetParkingBean;
import com.xinfu.newproject.bean.park_bean.bean.PaymentsBean;
import com.xinfu.newproject.bean.paycost.bean.ComputeParkingBean;
import com.xinfu.newproject.presenter.ParkingPresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;
import com.xinfu.newproject.widget.picker_view.OptionsPickerView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;
import static com.xinfu.newproject.contants.Contants.REQUEST_CODE_DEAL;

public class AddTemporaryParkFragment extends BaseActivity implements IView.IParkingView {

    @BindView(R.id.head_text)
    TextView titleTv;

    @BindView(R.id.head_back)
    ImageView backIv;

    @BindView(R.id.fragment_add_temporary_tv)
    TextView submitTv;

    @BindView(R.id.fragment_add_temporary_choose_lot)
    TextView chooseTv;

    @BindView(R.id.fragment_add_temporary_input_view)
    InputView inputView;

    @BindView(R.id.fragment_add_temporary_new_type)
    Button newTypeBtn;

    private PopupKeyboard mPopupKeyboard;

    private OptionsPickerView<String> communityPickView;
    //小区名称
    private ArrayList<String> optionsCommunities = new ArrayList<>();
    private ArrayList<String> optionsCommunitiesId = new ArrayList<>();
    private ArrayList<String> optionsCommunitiesHeaderId = new ArrayList<>();

    private ParkingPresenter parkingPresenter = new ParkingPresenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_add_temporary_park);
        ButterKnife.bind(this);
        registerBack();
        if (getIntent().getExtras().getInt("type") == 1) {
            setActivityTitle("添加月卡车");
            chooseTv.setVisibility(View.VISIBLE);
        } else if (getIntent().getExtras().getInt("type") == 2) {
            setActivityTitle("添加临停车");
            chooseTv.setVisibility(View.VISIBLE);
        }
        initViewBind();
        initLisener();

    }

    private void initLisener() {
        initOptionSelector();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initViewBind() {
        submitTv.setText("提交");
        // 创建弹出键盘
        mPopupKeyboard = new com.parkingwang.vehiclekeyboard.support.PopupKeyboard(this);
        // 弹出键盘内部包含一个KeyboardView，在此绑定输入两者关联。
        mPopupKeyboard.attach(inputView, this);
        mPopupKeyboard.getKeyboardView()
                .setKeyboardType(KeyboardType.CIVIL_WJ);

        // KeyboardInputController提供一个默认实现的新能源车牌锁定按钮
        mPopupKeyboard.getController()
                .setDebugEnabled(true)
                .bindLockTypeProxy(new KeyboardInputController.ButtonProxyImpl(newTypeBtn) {
                    @Override
                    public void onNumberTypeChanged(boolean isNewEnergyType) {
                        super.onNumberTypeChanged(isNewEnergyType);
                        if (isNewEnergyType) {
                            newTypeBtn.setText("切换为普通车");
                            newTypeBtn.setTextColor(getResources().getColor(R.color.yellow_ff));
                        } else {
                            newTypeBtn.setText("切换为新能源");
                            newTypeBtn.setTextColor(getResources().getColor(R.color.yellow_ff));
                        }
                    }
                });
        mPopupKeyboard.getKeyboardView().addKeyboardCallback(new KeyboardCallback() {
            @Override
            public void onTextKey(String text) {

            }

            @Override
            public void onDeleteKey() {

            }

            @Override
            public void onConfirmKey() {
                mPopupKeyboard.dismiss(AddTemporaryParkFragment.this);
            }

            @Override
            public void onUpdateKeyboard(KeyboardEntry keyboard) {

            }
        });
    }


    @OnClick({R.id.fragment_add_temporary_tv, R.id.fragment_add_temporary_choose_lot})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.fragment_add_temporary_tv:
                urlType = 2;
                if (isLogin()) {
                    if (inputView.getNumber().length() < 7) {
                        BToast.showText(this, "请输入完整的车牌号");
                    } else {
                        if (newTypeBtn.getText().toString().equals("切换为普通车") && inputView.getNumber().length() < 8) {
                            BToast.showText(this, "请输入完整的车牌号");
                            break;
                        }
                        if (getIntent().getExtras().getInt("type") == 1) {
                            if (parkLotId != null) {
                                parkingPresenter.checkMonthParkCar();
                            } else {
                                BToast.showText(this, "请选择停车场");
                            }
                        } else if (getIntent().getExtras().getInt("type") == 2) {
                            parkLotId = "";
                            parkingPresenter.checkMonthParkCar();
                        }
                    }
                }
                break;
            case R.id.fragment_add_temporary_choose_lot:
                urlType = 1;
                if (mPopupKeyboard != null) {
                    mPopupKeyboard.dismiss(this);
                }
                if (isLogin()) {
                    parkingPresenter.getParkingLot();
                }
                break;
        }
    }

    /**
     * 显示车场
     */
    private String parkLotId, tenantId, projectId;

    private void initOptionSelector() {
        communityPickView = new OptionsPickerView<>(this);
        communityPickView.setPicker(optionsCommunities);
        communityPickView.setTitle("请选择小区车场");
        communityPickView.setCyclic(false);
        communityPickView.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                chooseTv.setText(optionsCommunities.get(options1));
                parkLotId = optionsCommunitiesId.get(options1);
                tenantId = optionsCommunitiesHeaderId.get(options1);
                projectId = ggg.getResult().get(options1).getProjectId();
            }
        });

    }


    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;
    int urlType;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(this, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            BToast.showText(this, "请先登录");
            return false;
        }

        return true;
    }

    @Override
    public String header() {
        if (urlType == 1) {
            return token;
        } else if (urlType == 2) {
            return tenantId;
        }
        return token;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_DEAL:
                    if (data.getExtras().getInt("finish") == 1) {
                        finish();
                    }
                    break;
            }

        }
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String parkInfoId() {
        return parkLotId;
    }

    @Override
    public String carId() {
        return inputView.getNumber().toUpperCase();
    }

    @Override
    public Integer carType() {
        return getIntent().getExtras().getInt("type");
    }

    @Override
    public String projectId() {
        return null;
    }

    @Override
    public String exclusiveFlag() {
        return null;
    }

    @Override
    public String monthCarType() {
        return null;
    }

    @Override
    public String roomId() {
        return null;
    }

    @Override
    public String owner() {
        return null;
    }

    @Override
    public String mobile() {
        return null;
    }

    @Override
    public String images() {
        return null;
    }

    @Override
    public String remark() {
        return null;
    }

    @Override
    public Integer idType() {
        return null;
    }

    @Override
    public String filed1() {
        return null;
    }

    @Override
    public String id() {
        return null;
    }

    @Override
    public String startTime() {
        return null;
    }

    @Override
    public String orderId() {
        return null;
    }

    @Override
    public String licensePlate() {
        return null;
    }

    @Override
    public void showToast(String content) {
        BToast.showText(this, content);
    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void showProgress() {
        showLoding();
    }

    @Override
    public void hideProgress() {
        hideLoding();
    }

    GetParkingBean ggg;

    @Override
    public void getMsg(GetParkingBean getParkingBean) {
        optionsCommunities.clear();
        optionsCommunitiesId.clear();
        optionsCommunitiesHeaderId.clear();
        ggg = getParkingBean;

        for (int i = 0; i < getParkingBean.getResult().size(); i++) {
            if (getParkingBean.getResult().get(i).getParkName() != null && getParkingBean.getResult().get(i).getId() != null && getParkingBean.getResult().get(i).getTenantId() != null) {
                optionsCommunities.add(getParkingBean.getResult().get(i).getParkName());
                optionsCommunitiesId.add(getParkingBean.getResult().get(i).getId());
                optionsCommunitiesHeaderId.add(getParkingBean.getResult().get(i).getTenantId());
            }

        }
        if (communityPickView != null) {
            communityPickView.show();
        }
    }

    @Override
    public void getMsg(CheckMonthParkBean checkMonthParkBean) {
        if (checkMonthParkBean.getResult().getCheckStatus() != -3) {
            BToast.showText(this, "" + checkMonthParkBean.getResult().getCheckStatus());
        } else {
            if (getIntent().getExtras().getInt("type", 0) == 1) {
                Bundle bundle = new Bundle();
                bundle.putString("carId", inputView.getNumber().toUpperCase());
                bundle.putString("parkInfoId", parkLotId);
                bundle.putString("parkName", chooseTv.getText().toString());
                bundle.putString("tenantId", tenantId);
                bundle.putString("projectId", projectId);
                bundle.putInt("carType", 1);
                bundle.putString("checkMonthParkBean", new Gson().toJson(checkMonthParkBean));
                startIntentActivityForResult(AddMonthCardParkFragment.class, bundle, REQUEST_CODE_DEAL);
            }
        }

    }

    @Override
    public void getMsg(AddMonthCarBean addMonthCarBean) {

    }

    @Override
    public void getMsg(GetMonthCarListBean getMonthCarListBean) {

    }

    @Override
    public void getMsg(PaymentsBean paymentsBean) {

    }

    @Override
    public void getMsg(ComputeParkingBean computeParkingBean) {

    }

    @Override
    public void getMsg(FeeListBean feeListBean) {

    }
}
